define(function (require, exports, module) {
    var util = require('util');
    var ui = require('ui');
    var common = require('./common');
    var angular = require("angular");
    var dialogComp = require("dialog");
    require("angular-route");
    require("angular-ui");
    require('authorize');
    require('service/ng_service/projectService');
    require('./project_reported_rules');
    require('./project_houses_param');
    require('./project_commission_rules');
    require('permission');
    require('global');

    angular.module('app', ['ngRoute', 'ngUI', 'ProjectReportedRules', 'ProjectHousesParam', 'ProjectCommissionRules'])
        .config(['$routeProvider', function ($routeProvider) {
            $routeProvider
                .when('/reported_rules', {templateUrl: 'inc/reported_rules.html'})
                .when('/houses_param', {templateUrl: 'inc/houses_param.html'})
                .when('/commission_rules', {templateUrl: 'inc/commission_rules.html'})
                .otherwise({redirectTo: '/reported_rules'});
        }])
        .controller('MainController', ['$rootScope', '$scope', '$http', '$location', '$routeParams', 'projectList',
            function ($rootScope, $scope, $http, $location, $routeParams, projectList) {
                $scope.tabs = [
                    {value: '/reported_rules', name: '报备规则'},
                    {value: '/houses_param', name: '楼盘参数设置'},
                    {value: '/commission_rules', name: '佣金规则'}
                ];

                $scope.current = $location.path();
                $rootScope.$on('$routeChangeSuccess', function () {
                    $scope.current = $location.path();
                });

                projectList.then(
                    function (res) {
                        var projectList = res.data.data;

                        $scope.projectList = projectList;
                        $scope.onShowCloneTips = function (item, dialog, dialogScope) {
                            dialog._$('content').css({height:280,overflowY:'auto',overflowX:'hidden',display:'block'})
                            $scope.targetProjectList = [];
                            dialogScope.projectList = _.filter($scope.projectList, function (item, value) {
                                item.selected = false;
                                if (item.p_projectId != $scope.curProjectId) {
                                    return true;
                                } else {
                                    dialogScope.clone_source = item;
                                    return false;
                                }
                            });
                            $scope.targetProjectList = dialogScope.projectList;
                        }

                        $scope.current = $location.path();
                    }
                );
                var dialogHandler, errorMessage = {};

                function showConfirmTips(msg, callback) {
                    errorMessage[msg] = msg;
                    if (!dialogHandler) {
                        dialogHandler = dialogComp({
                            title: '提示',
                            fixed: true,
                            content: msg + '',
                            width: 350,
                            okValue: '确定',
                            cancelDisplay: true,
                            ok: function () {
                                dialogHandler = null;
                                errorMessage = {};
                                if (callback) {
                                    callback();
                                }

                            },
                            cancel: function () {
                                dialogHandler = null;
                                errorMessage = {};
                            }
                        }).showModal();
                    } else {
                        var margeMsg = msg;
                        dialogHandler.content(margeMsg);
                        dialogHandler.reset();
                    }
                }

                /**
                 * 获取选中的克隆目标项目
                 * @returns {Array}
                 */
                function getTargetProject() {
                    var res = [];
                    var proj;
                    for (var i = 0; proj = $scope.targetProjectList[i]; i++) {
                        if (proj.selected) {
                            res.push(proj);
                        }
                    }
                    return res;
                }

                $scope.getTargetProject = getTargetProject;
                /**
                 * 克隆项目
                 * @param item
                 * @param dialog
                 * @param dialogScope
                 * @returns {boolean}
                 */
                $scope.cloneProjects = function (item, dialog, dialogScope) {

                    var targetProject = $scope.getTargetProject();
                    targetProject = _.pluck(targetProject, 'p_projectId');
                    if(targetProject.length>5){showConfirmTips('一次最多支持5个项目，以免克隆时间过长！');return false;}
                    if (targetProject.length > 0) {
                        showConfirmTips('克隆项目有可能会覆盖原有配置，确认是否克隆？', function () {
                            var data = {
                                "USE_FORM_DATA": false,
                                sourceProId: $scope.curProjectId,
                                goalProIds: targetProject.join()
                            };
                            $http.post('/api/index.php?r=qmyx/para-setting/project-clone', data).success(function (data, status) {
                                if (data.retCode == 0) {
                                    dialog.close();
                                    ui.showSucTip('克隆成功');
                                } else {
                                    showConfirmTips(data.errMsg || '克隆项目出错！');
                                }
                            }).error(function (data, status) {
                                if (data.errMsg) {
                                    showConfirmTips(data.errMsg);
                                }
                            })
                        });

                    } else {
                        showConfirmTips('请选择需要克隆的目标项目！');
                    }

                    return false;
                }
            }
        ]);

    angular.bootstrap(document, ['app']);
});